package com.skynet.supervision.utils;

import org.apache.http.entity.ContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

@Component
public class WordUtils {

    @Autowired
    private FilesUtils filesUtils;




    /**
     * 百分号编码工具方法
     * @param s 需要百分号编码的字符串
     * @return 百分号编码后的字符串
     */
    public static String percentEncode(String s) throws UnsupportedEncodingException {
        String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
        return encode.replaceAll("\\+", "%20");
    }


    public static void download(String tmpPath, HttpServletResponse res,String name) throws IOException {
        // 设置信息给客户端不解析
//        String type = new MimetypesFileTypeMap().getContentType("docx");
        // 设置contenttype，即告诉客户端所发送的数据属于什么类型
        res.setHeader("Content-type","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        // 设置编码
        String fileName = System.currentTimeMillis() + ".docx";
        File file = new File(tmpPath);
        res.setHeader("Content-Disposition", "attachment;filename="+fileName+"."+ name);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        int i = bis.read(buff);
        OutputStream outputStream=res.getOutputStream();
        while (i != -1) {
            outputStream.write(buff, 0, buff.length);
            outputStream.flush();
            i = bis.read(buff);
        }
        file.delete();

    }

    //下载文件同时保存到本地服务器
    public String downloadLocalSave(String tmpPath, HttpServletResponse res,String name) throws IOException {
        // 设置信息给客户端不解析
//        String type = new MimetypesFileTypeMap().getContentType("docx");
        // 设置contenttype，即告诉客户端所发送的数据属于什么类型
        res.setHeader("Content-type","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        // 设置编码
        String fileName = System.currentTimeMillis() + ".docx";
        File file = new File(tmpPath);
        res.setHeader("Content-Disposition", "attachment;filename="+fileName+"."+ name);
        byte[] buff = new byte[1024];
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        int i = bis.read(buff);
        OutputStream outputStream=res.getOutputStream();
        while (i != -1) {
            outputStream.write(buff, 0, buff.length);
            outputStream.flush();
            i = bis.read(buff);
        }
        //输入流
        FileInputStream inputStream = new FileInputStream(file);
        MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream);
        String upload = this.filesUtils.upload(multipartFile);
        file.delete();
        return upload;

    }
}
